package net.microfigure.service.impl;

import net.microfigure.model.ExpInst;
import net.microfigure.model.ZcPolicy;
import net.microfigure.commons.result.PageInfo;
import net.microfigure.mapper.ExpInstMapper;
import net.microfigure.service.IExpInstService;
import net.microfigure.service.IZcPolicyService;

import java.math.BigDecimal;
import java.util.Calendar;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;


/**
 * <p>
 * 报销实例表 服务实现类
 * </p>
 *
 * @author 林景锋
 * @since 2018-06-25
 */
@Service
public class ExpInstServiceImpl extends ServiceImpl<ExpInstMapper, ExpInst> implements IExpInstService {

	
	@Autowired
	private IZcPolicyService  zcPolicyService;
	
	@Override
	public ExpInst preSubmitExpense(ExpInst expInst) {
		//获取当前年份政策
		int currentYear = Calendar.getInstance().get(Calendar.YEAR);
		System.out.println("获取当前年份政策"+currentYear);
		ZcPolicy zcPolicy = zcPolicyService.selectRunyearEqualCurrentYear(currentYear);
		System.out.println(zcPolicy.getRunRate());
		//计算报销金额
		BigDecimal bxPay = expInst.getAllCost().multiply(zcPolicy.getRunRate());
		//如果大于封顶线，则报销金额等于封顶线
		if(bxPay.compareTo(zcPolicy.getMaxLine()) == 1) {
			bxPay = zcPolicy.getMaxLine();
		}
		System.out.println("计算报销金额"+bxPay);
		expInst.setBxPay(bxPay);
		return expInst;
	}

	@Override
	public void submitExpense(ExpInst expInst) {
		// TODO Auto-generated method stub

	}

	@Override
	public void selectDataGrid(PageInfo pageInfo) {
		Page<ExpInst> page = new Page<ExpInst>(pageInfo.getNowpage(), pageInfo.getSize());
		EntityWrapper<ExpInst> wrapper = new EntityWrapper<ExpInst>();
		wrapper.orderBy(pageInfo.getSort(),pageInfo.getOrder().equalsIgnoreCase("ASC"));
		selectPage(page,wrapper);
		pageInfo.setRows(page.getRecords());
		pageInfo.setTotal(page.getTotal());
		
	}

}
